/*
 * @LastEditTime: 2023-03-31 23:31:24
 * @Method: 
 * @Description: 
 */
import java.util.*;

package CodeTop;

public class Solution113 {
    List<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        dfs(root, 0, targetSum);
        return res;
    }

    public void dfs (TreeNode root, int sum, int target) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null && sum + root.val == target) {
            path.add(root.val);
            res.add(new LinkedList<>(path));
            path.removeLast();
            return;
        }
        path.add(root.val);
        dfs(root.left, sum + root.val, target);
        dfs(root.right, sum + root.val, target);
        path.removeLast();
    }
}
